001    package net.sf.xdc.processing;
002    
003    /*
004     *  Copyright 2005-2006 Jens Voß.
005     *
006     *  Licensed under the GNU Lesser General Public License (the "License");
007     *  you may not use this file except in compliance with the License.
008     *  You may obtain a copy of the License at
009     *
010     *       http://opensource.org/licenses/lgpl-license.php
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    import java.util.Properties;
020    import java.util.Iterator;
021    import java.util.Date;
022    import java.nio.charset.Charset;
023    import java.nio.charset.UnsupportedCharsetException;
024    
025    import org.apache.commons.cli.Options;
026    import org.apache.commons.cli.Option;
027    import org.apache.commons.cli.CommandLine;
028    import org.apache.log4j.Logger;
029    import net.sf.xdc.util.Logging;
030    
031    /**
032     * This class is responsible for setting up all available command line options
033     * for the XDC tool. It implements the Singleton design pattern.
034     *
035     * @author Jens Voß
036     * @since 0.5
037     * @version 0.5
038     */
039    public class XdcOptions extends Options {
040    
041      private static final Logger LOG = Logging.getLogger();
042    
043      private static final XdcOptions XDC_OPTIONS = new XdcOptions();
044    
045      /**
046       * Static getter method for the (single) instance of the <code>XdcOptions</code>
047       * class.
048       *
049       * @return The instance of the <code>XdcOptions</code> class.
050       */
051      public static XdcOptions getXdcOptions() {
052        return XDC_OPTIONS;
053      }
054    
055      private XdcOptions() {
056    
057        addXdcOption("author", "Include @author paragraphs", true);
058        addXdcOption("bottom", "html-code", "Include bottom text for each page", true);
059        addXdcOption("charset", "charset", "Charset for cross-platform viewing of generated documentation", true);
060        addXdcOption("d", "directory", "Destination directory for output files", false);
061        addXdcOption("defaultexcludes", "Exclude default files and directories from processing", false);
062        addXdcOption("dialect", "xmldialect", "Specify XML dialect for all source files", false);
063        addXdcOption("dialects", "file", "Specify XML dialects file", false);
064        addXdcOption("dialectmapping", "file", "Specify XML dialect mapping file", false);
065        addXdcOption("docencoding", "name", "Output encoding name", false);
066        addXdcOption("doctitle", "html-code", "Include title for the overview page", true);
067        addXdcOption("encoding", "name", "Source file encoding name", false);
068        addXdcOption("exclude", "pkglist", "Specify a list of packages to exclude", false);
069        addXdcOption("extensions", "extensionlist", "Specify file extensions of source files", false);
070        addXdcOption("footer", "html-code", "Include footer text for each page", true);
071        addXdcOption("forceoptions", "Override processing instructions with options", true);
072        addXdcOption("header", "html-code", "Include header text for each page", true);
073        addXdcOption("help", "Display command line options and exit", false);
074        addXdcOption("helpfile", "file", "Include file that help link links to", false);
075        addXdcOption("J<flag>", "Pass <flag> directly to the runtime system", false);
076        addXdcOption("linksource", "Generate source in HTML", true);
077        addXdcOption("locale", "name", "Locale to be used, e.g. en_US", false);
078        addXdcOption("nohelp", "Do not generate help link", true);
079        addXdcOption("nonavbar", "Do not generate navigation bar", true);
080        addXdcOption("nosince", "Do not include @since information", true);
081        addXdcOption("notimestamp", "Do not include hidden time stamp", true);
082        addXdcOption("overview", "file", "Read overview documentation from HTML file", false);
083        addXdcOption("reportmissing", "Report missing XDC documentation in source files", true);
084        addXdcOption("sourcepath", "pathlist", "Specify where to find source files", false);
085        addXdcOption("stylesheetfile", "path", "File to change style of the generated documentation", false);
086        addXdcOption("subpackages", "subpkglist", "Specify subpackages to recursively load", false);
087        addXdcOption("version", "Include @version paragraphs", true);
088        addXdcOption("windowtitle", "text", "Browser window title for the documenation", true);
089      }
090    
091      private void addXdcOption(String opt, String description, boolean xslParam) {
092        addOption(new XdcOption(opt, description, xslParam));
093      }
094    
095      private void addXdcOption(String opt, String argName, String description, boolean xslParam) {
096        addOption(new XdcOption(opt, argName, description, xslParam));
097      }
098    
099      /**
100       * This method retrieves an available XDC option by its name. Unlike its
101       * <code>super</code> method, it is case-insensitive.
102       *
103       * @param opt The name of the XDC command line option to be retrieved
104       *         (case-insensitive)
105       * @return The XDC command line option with the specified name
106       */
107      public Option getOption(String opt) {
108        return super.getOption(opt.toLowerCase());
109      }
110    
111      /**
112       * This method determines whether an option with a certain name is available.
113       * Unlike its <code>super</code> method, this method is case-insensitive.
114       *
115       * @param opt The name of the XDC command line option in question
116       * @return <b>true</b> if the XDC command line option with the specified
117       *          name is available; <b>false</b> otherwise
118       */
119      public boolean hasOption(String opt) {
120        return super.hasOption(opt.toLowerCase());
121      }
122    
123      /**
124       * This method is used to convert all available XDC options for which a value
125       * has been set on the command line into a <code>Properties</code> object.
126       * Only those options for which {@link XdcOption#isXslParam()} is true are
127       * included in the return value.
128       *
129       * @param line The command line object which may or may not specify values
130       *         for the available XDC options
131       * @return A <code>Properties</code> object which contains key-value pairs
132       *          for all available XDC options which have been set on the command
133       *          line and whose {@link XdcOption#isXslParam()} method returns
134       *          <b>true</b>
135       */
136      public Properties getOptionProperties(CommandLine line) {
137        Properties retVal = new Properties();
138        for (Iterator iter = getOptions().iterator(); iter.hasNext();) {
139          XdcOption option = (XdcOption) iter.next();
140          if (option.isXslParam()) {
141            String opt = option.getOpt().toLowerCase();
142            if (line.hasOption(opt)) {
143              retVal.setProperty(opt, option.hasArg() ? line.getOptionValue(opt) : "1");
144            }
145          }
146        }
147        if (retVal.containsKey("charset")) {
148          try {
149            Charset charset = Charset.forName(retVal.getProperty("charset"));
150            retVal.setProperty("charset", charset.name());
151          }
152          catch (UnsupportedCharsetException e) {
153            LOG.warn("Charset '" + retVal.getProperty("charset") + "' not recognized. Charset is ignored.");
154            retVal.remove("charset");
155          }
156        }
157        retVal.setProperty("timestamp", new Date().toString());
158        return retVal;
159      }
160    
161    }